<?php
/**
 * This page handles any errors
 * 
 * Apache is setup as follows:
 * Apache Config is set to send Error 404 to error_handler.php?e=404
 * Apache Config is set to send Error 403 to error_handler.php?e=403
 * Apache Config is set to send Error 500 to error_handler.php?e=500
 * 
 * This page can also be used as a fallback method for displaying an error 500 without relying on apache or libdebug,
 *	or as a way to generate 404 or 403 gracefully.
 * 
 * This can be done by setting the variable $gVAR['e'] to 403, 404 or 500. (Other codes will yield a 404).
 * Then include this file require_once('error_handler.php');
 * This file will then handle the rest of the content generation.
 * 
 * NOTE:    This fallback method must be used WITHOUT rendering your own template. ($TemplateEngine->render());
 *	    Otherwise, you will get a double page or an incorrect render, or an Apache 500.
 * 
 */
    
    if(isset($_ECLIPSEMDE['ROOT_WD']) === true) {
	require_once($_ECLIPSEMDE['ROOT_WD'].'SiteIncludes.GLOBAL.php');
    } else {
	require_once('../SiteIncludes.GLOBAL.php');
    }
    $Templater = $TemplateEngine->getTemplater();
    if($gVAR['e'] == '403') {
	$error_code = ((int)$gVAR['e']);
	$error_header = 'HTTP/1.1 403 Forbidden';
	$error_html = $Templater->makeherounit('403 :: Woops!', 'Sorry, You do not have the permissions to do that.', '<a class="btn btn-primary btn-large" href="/">Home Page</a>');
    } elseif($gVAR['e'] == '500') {
	$error_code = ((int)$gVAR['e']);
	$error_header = 'HTTP/1.1 500 Internal Server Error';
	$error_html = $Templater->makeherounit('500 :: Woops!', 'Our Bad! Something went wrong on our end, please try again later', '<a class="btn btn-primary btn-large" href="/">Home Page</a>');
    } else {
	$error_code = ((int)$gVAR['e']);
	$error_header = 'HTTP/1.1 404 Not Found';
	$error_html = $Templater->makeherounit('404 :: Woops!', 'That page, article or tool could not be found!', '<a class="btn btn-primary btn-large" href="/">Home Page</a>');
    }
    
    /**
     * Special Handler for .htaccess causing a 404
     */
    if(isset($_SERVER['REDIRECT_REDIRECT_STATUS']) === true && $_SERVER['REDIRECT_REDIRECT_STATUS'] == "200") {
	if(isset($_SERVER['REDIRECT_STATUS']) === true && $_SERVER['REDIRECT_STATUS'] == "404") {
	    /*
	     * This error here is where Apache has redirected a call to a non-existant file from .htaccess.
	     * This is a double error 404. This is ACTUALLY a 500
	     */
	    $error_code = ((int)$gVAR['e']);
	    $error_header = 'HTTP/1.1 500 Internal Server Error';
	    trigger_error("INCORRECT CONFIGURATION! Double 404 Error from .htaccess! <pre>".print_r($_SERVER, true)."</pre>", E_USER_WARNING);
	    $error_html = $Templater->makeherounit('500 :: Woops!', 'This should actually have worked. But we got something wrong. Please report this error and the link you used to get here!<br /><b>Please include this information</b>: "<i>'.$_SERVER['REQUEST_URI'].'</i>"', '<a class="btn btn-primary btn-large" href="/">Home Page</a>');
	}
    }
    
    
    header($error_header);
    $TemplateEngine->destroyContent();
    
    // Replace NavBar items
    require($_ECLIPSEMDE['ROOT_WD'].'sitewide_template_includes/NavigationBar.php');
    $TemplateEngine->create_content_html(
	    $error_html
    );
    
    $TemplateEngine->setHasSidebar(false);
    
    $TemplateEngine->render();
    die();
?>